package com.cn.webspider;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class WebMagicSaveField implements PageProcessor {
    private Site site = Site.me().setRetryTimes(5).setSleepTime(1000);

    @Override
    public void process(Page page) {
        String url = page.getUrl().get();
        if (StringUtils.isNoneBlank(url) && url.startsWith("http://www.mmonly.cc/ktmh/list")) {
            //把当前页所有图片都添加到请求目标中去，进去详情页下载图片
            page.addTargetRequests(page.getHtml().$("#infinite_scroll > div > div > div > div.ABox > a").links().all());
            //添加下一个连接到目标连接中  获取到倒数第二个  下一页
            String html = page.getHtml().$("#pageNum > a").all().stream().filter(x -> x.contains("下一页")).findFirst().toString();
            page.addTargetRequest("http://www.mmonly.cc/ktmh/" + html.substring(html.indexOf('"') + 1, html.lastIndexOf('"')));
        } else if (StringUtils.isNoneBlank(url) && url.startsWith("http://www.mmonly.cc/ktmh/dmmt/")) {
            //这个详情页面可能存在这一系列图片不止一张的情况，多张图片有下一页
            Selectable detailNexUrl = page.getHtml().$("#nl").links();
            if (StringUtils.isNotBlank(detailNexUrl.get()) && !"##".equals(detailNexUrl.get())) {
                page.addTargetRequest(detailNexUrl.get());
            }

            //具体的保存图片
//            Selectable picture = page.getHtml().$("#big-pic > p > a >img");
//            String picUrl = picture.links().get();
//            String picName = picture.;
//            String picUrl = page.getHtml().xpath("//div[@id=big-pic]/p/a/img/@src").get();
//            String picName = page.getHtml().xpath("//div[@id=big-pic]/p/a/img/@alt").get();
//            System.out.println(picName + "-------------" + picUrl);
            page.putField("picUrl", page.getHtml().xpath("//div[@id=big-pic]/p/a/img/@src").get());
            page.putField("picName", page.getHtml().xpath("//div[@id=big-pic]/p/a/img/@alt").get());
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    @Test
    public void picture() {
        Spider
                .create(new WebMagicSaveField())
                .addUrl("http://www.mmonly.cc/ktmh/list_28_1.html")
                .thread(100)
//                .setScheduler(new )
                .addPipeline(new DownloadPicture())
                .run();
    }
}
